package com.syh.jn.scm.infrastructure.repository.impl.supplier.base.downstream;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.util.other.ExtStringUtils;
import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.infrastructure.repository.AbstractAggregateRepository;
import com.syh.core.expction.DataModelException;
import com.syh.jn.scm.domain.supplier.entity.base.downstream.DownstreamCategory;
import com.syh.jn.scm.domain.supplier.repository.base.downstream.DownstreamCategoryRepository;
import com.syh.jn.scm.dto.supplier.base.downstream.DownstreamCategoryVo;
import com.syh.jn.scm.infrastructure.repository.model.base.downstream.DownstreamCategoryModel;
import com.syh.jn.scm.infrastructure.repository.utils.SQLAliasGenerateUtil;
import com.syh.jn.scm.infrastructure.wrapper.BaseResolve;
import com.syh.jn.scm.infrastructure.wrapper.ResolveToSQL;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

@Repository
public class DownstreamCategoryRepositoryImpl extends AbstractAggregateRepository<DownstreamCategory, DownstreamCategoryModel, Long> implements DownstreamCategoryRepository {
  @Override
  public boolean haveSubCates(Long id) {
    return false;
  }

  @Override
  protected SQL getViewSql() {
    return new SQL() {{
      SELECT(
              SQLAliasGenerateUtil.aliasGenerate(DownstreamCategoryModel.class, "cate1", null),
              "cate2.name as supName"
      );
      FROM(getModelTableName(DownstreamCategoryModel.class) + " cate1");
      LEFT_OUTER_JOIN(getModelTableName(DownstreamCategoryModel.class) + " cate2 on cate1.SUP_ID = cate2.ID");
    }};
  }

  @Override
  protected Class getViewObject() {
    return DownstreamCategoryVo.class;
  }

  @Override
  protected BaseResolve getViewWrapperResolve(Wrapper wrapper) {
    Map<String, String> params = new HashMap<>();
    params.put("name", "cate1.NAME");
    params.put("status", "cate1.STATUS");
    params.put("code", "cate1.CODE");
    params.put("id", "cate1.ID");
    params.put("businessEntityId", "cate1.BUSINESS_ENTITY_ID");
    ResolveToSQL parse = new ResolveToSQL(wrapper, (propertyName) -> {
      return params.get(propertyName);
    });
    return parse;
  }

  @Override
  public Long add(DownstreamCategory downstreamCategory) {
    ExtAssert.notNull(downstreamCategory, "分类对象不存在");
    DownstreamCategoryModel father = new DownstreamCategoryModel();
    if (!Long.valueOf("0").equals(downstreamCategory.getSupId())) {
      father = super.hibernateTemplate.get(DownstreamCategoryModel.class, downstreamCategory.getSupId());
      if (father == null) {
        throw new DataModelException("父级分类不存在");
      }
    }

    DownstreamCategoryModel cateModel = new DownstreamCategoryModel();
    ExtBeanUtils.copyProperties(downstreamCategory, cateModel);
    downstreamCategory.setId((Long) this.hibernateTemplate.save(cateModel));
    StringBuffer catalogCodeStr = ExtStringUtils.isEmpty(father.getCatalogCode()) ? new StringBuffer() : new StringBuffer(father.getCatalogCode()).append("-");
    cateModel.setCatalogCode(catalogCodeStr.append(downstreamCategory.getId()).toString());
    // 如果自定义code为空 直接使用id补充到6位
    if (ExtStringUtils.isEmpty(cateModel.getCode())) {
      cateModel.setCode(String.format("%06d", cateModel.getId()));
    }
    return downstreamCategory.getId();
  }

  @Override
  public void update(DownstreamCategory downstreamCategory) {
    ExtAssert.notNull(downstreamCategory, "分类对象不存在");
    StringBuffer catalogCodeSb = null;
    if (!Long.valueOf("0").equals(downstreamCategory.getSupId())) {
      DownstreamCategoryModel father = super.hibernateTemplate.get(DownstreamCategoryModel.class, downstreamCategory.getSupId());
      if (father == null) {
        throw new DataModelException("父级分类不存在");
      }
      catalogCodeSb = ExtStringUtils.isEmpty(father.getCatalogCode()) ? new StringBuffer() : new StringBuffer(father.getCatalogCode()).append("-").append(downstreamCategory.getId());
    } else {
      catalogCodeSb = new StringBuffer().append(downstreamCategory.getId());
    }

    DownstreamCategoryModel cateModel = this.hibernateTemplate.get(DownstreamCategoryModel.class, downstreamCategory.getId());
    ExtBeanUtils.copyProperties(downstreamCategory, cateModel);
    // 如果自定义code为空 直接使用id补充到6位
    if (ExtStringUtils.isEmpty(cateModel.getCode())) {
      cateModel.setCode(String.format("%06d", cateModel.getId()));
    }
    cateModel.setCatalogCode(catalogCodeSb.toString());

    this.hibernateTemplate.update(cateModel);
  }
}
